import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* 静态路由 */
export const constantRoutes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/alipay',
    name: 'alipay',
    meta: { title: '支付', icon: 'alipay' },
    component:() => import('@/views/alipay/index')
  },
  {
    path: '/editPwd',
    name: 'editPwd',
    component: Layout,
    hidden: true
  },
  {
    path: '/recharge',
    name: 'recharge',
    component: () => import('@/views/recharge/index'),
    hidden: true
  },
  {
    path: '/rechargeCode',
    name: 'rechargeCode',
    component: () => import('@/views/recharge/code'),
    hidden: true
  },
  {
    path: '/rechargeSuccess',
    name: 'rechargeSuccess',
    component: () => import('@/views/recharge/success'),
    hidden: true
  },
  {
    path: '/apply',
    name: 'apply',
    component: () => import('@/views/apply/index'),
    hidden: true
  },
  {
    path: '/trans',
    component: () => import('@/views/trans'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/trans',
    hidden: true
  }
]

/* 动态路由 */
export const asyncRoutes = [
  {
    path: '/service',
    name: 'service',
    component: Layout,
    redirect: '/service/contract',
    meta: { title: '增值服务', icon: 'service' },
    children: [
      {
        path: 'message',
        name: 'serviceMessage',
        component: () => import('@/views/service/message'),
        meta: { title: '短信提醒', icon: '', keepAlive: true }
      },
      {
        path: 'messageRecord',
        name: 'serviceMessageRecord',
        hidden: true,
        component: () => import('@/views/service/messageRecord'),
        meta: {
          title: '短信购买记录',
          icon: '',
          activeMenu: '/service/message',
          parentTitle: '短信提醒',
          parentRouteName: 'serviceMessage'
        }
      },
      {
        path: 'risk',
        name: 'serviceRisk',
        component: () => import('@/views/service/risk'),
        meta: { title: '百融风控', icon: '' }
      },
      {
        path: 'riskRecord',
        name: 'serviceRiskRecord',
        hidden: true,
        component: () => import('@/views/service/riskRecord'),
        meta: {
          title: '风控记录',
          icon: '',
          activeMenu: '/service/risk',
          parentTitle: '百融风控',
          parentRouteName: 'serviceRisk'
        }
      },
      {
        path: 'rickWong',
        name: 'serviceRickWong',
        component: () => import('@/views/service/rickWong'),
        meta: { title: '征信系统', icon: '' }
      },
        //audience
      {
        path: 'audience',
        name: 'serviceAudience',
        component: () => import('@/views/service/audience'),
        meta: { title: '生成律师函', icon: '' }
      },
    ]
  },
  {
    path: '/order',
    name: 'order',
    component: Layout,
    redirect: '/order/list',
    meta: { title: '订单管理', icon: 'order' },
    children: [
      {
        path: 'list',
        name: 'orderList',
        component: () => import('@/views/order/list'),
        meta: { title: '订单列表', icon: '' }
      },
      {
        path: 'index',
        name: 'orderbook',
        component: () => import('@/views/order/index'),
        meta: { title: '逾期列表', icon: '' }
      },
      {
        path: 'statement ',
        name: 'orderstatement',
        component: () => import('@/views/order/statement'),
        meta: { title: '账单日列表', icon: '' }
      },
      // {
      //   path: 'buyout',
      //   name: 'orderBuyout',
      //   component: () => import('@/views/order/buyout'),
      //   meta: { title: '买断订单', icon: '' }
      // },
      // {
      //   path: 'unpay',
      //   name: 'orderUnpay',
      //   component: () => import('@/views/order/unpay'),
      //   meta: { title: '签约待支付订单', icon: '' }
      // },
      // {
      //   path: 'relet',
      //   name: 'orderRelet',
      //   component: () => import('@/views/order/relet'),
      //   meta: { title: '续租订单', icon: '' }
      // }
    ]
  },
  {
    path: '/refund',
    name: 'refund',
    component: Layout,
    redirect: '/refund/list',
    meta: { title: '退款管理', icon: 'refund' },
    children: [
      {
        path: 'list',
        name: 'refundList',
        component: () => import('@/views/refund/list'),
        meta: { title: '仅退款列表', icon: '' }
      },
      {
        path: 'refundDetails/:id',
        name: 'refundDetail',
        hidden: true,
        props: true,
        component: () => import('@/views/refund/refundDetails'),
        meta: {
          title: '退款详情',
          icon: '',
          activeMenu: '/refund/list',
          parentTitle: '仅退款列表',
          parentRouteName: 'refund'
        }
      }
    ]
  },
  {
    path: '/goods',
    name: 'goods',
    component: Layout,
    redirect: '/goods/index',
    meta: { title: '商品管理', icon: 'product' },
    children: [
      {
        path: 'index',
        name: 'goodsIndex',
        component: () => import('@/views/goods/index'),
        meta: { title: '商品列表', icon: '', keepAlive: true }
      },
      {
        path: 'release/:draftId?',
        name: 'goodsAdd',
        hidden: true,
        props: true,
        component: () => import('@/views/goods/add'),
        meta: {
          title: '发布商品',
          icon: '',
          activeMenu: '/goods/index',
          parentTitle: '商品列表',
          parentRouteName: 'goodsIndex'
        }
      },
      {
        path: 'view/:goodsId',
        name: 'goodsView',
        hidden: true,
        props: true,
        component: () => import('@/views/goods/view'),
        meta: {
          title: '查看商品',
          icon: '',
          activeMenu: '/goods/index',
          parentTitle: '商品列表',
          parentRouteName: 'goodsIndex'
        }
      },
      {
        path: 'success/:goodsId/:isEdit?',
        name: 'goodsSuccess',
        hidden: true,
        props: true,
        component: () => import('@/views/goods/success'),
        meta: {
          title: '发布商品',
          icon: '',
          activeMenu: '/goods/index',
          parentTitle: '商品列表',
          parentRouteName: 'goodsIndex'
        }
      },
      {
        path: 'draft',
        name: 'goodsDraft',
        hidden: true,
        component: () => import('@/views/goods/draft'),
        meta: {
          title: '草稿箱',
          icon: '',
          activeMenu: '/goods/index',
          parentTitle: '商品列表',
          parentRouteName: 'goodsIndex'
        }
      },
      {
        path: 'baseEdit/:goodsId',
        name: 'goodsBaseEdit',
        hidden: true,
        props: true,
        component: () => import('@/views/goods/edit'),
        meta: {
          title: '编辑商品信息',
          icon: '',
          activeMenu: '/goods/index',
          parentTitle: '商品列表',
          parentRouteName: 'goodsIndex'
        }
      },
      {
        path: 'meal/:goodsId/:pact_type/:mealId?',
        name: 'goodsMeal',
        hidden: true,
        props: true,
        component: () => import('@/views/goods/edit'),
        meta: {
          title: '编辑套餐',
          icon: '',
          activeMenu: '/goods/index',
          parentTitle: '商品列表',
          parentRouteName: 'goodsIndex'
        }
      },
      {
        path: 'mealInfo/:goodsId',
        name: 'goodsMealInfo',
        hidden: true,
        props: true,
        component: () => import('@/views/goods/mealList'),
        meta: {
          title: '套餐信息',
          icon: '',
          activeMenu: '/goods/index',
          parentTitle: '商品列表',
          parentRouteName: 'goodsIndex'
        }
      },
      {
        path: 'category',
        name: 'goodsCategory',
        component: () => import('@/views/category/index'),
        meta: { title: '品类管理', icon: '' }
      },
      {
        path: 'add',
        name: 'goodsCategoryApply',
        hidden: true,
        component: () => import('@/views/category/add'),
        meta: {
          title: '申请品类',
          icon: '',
          activeMenu: '/goods/category',
          parentTitle: '品类管理',
          parentRouteName: 'goodsCategory'
        }
      }
    ]
  },
  {
    path: '/account',
    name: 'account',
    component: Layout,
    redirect: '/account/list',
    meta: { title: '账号管理', icon: 'user' },
    children: [
      {
        path: 'list',
        name: 'accountList',
        component: () => import('@/views/system/account/index'),
        meta: { title: '账号列表', icon: '' }
      }
    ]
  },
  {
    path: '/permission',
    name: 'permission',
    component: Layout,
    redirect: '/permission/role',
    meta: { title: '权限管理', icon: 'permission' },
    children: [
      {
        path: 'role',
        name: 'permissionRole',
        component: () => import('@/views/system/role/index'),
        meta: { title: '角色列表', icon: '' }
      }
    ]
  },
  {
    path: '/shop',
    name: 'shop',
    component: Layout,
    redirect: '/shop/returnAd',
    meta: { title: '店铺管理', icon: 'shop' },
    children: [
      {
        path: 'returnAd',
        name: 'shopReturnAd',
        component: () => import('@/views/shop/returnAd'),
        meta: { title: '归还地址设置', icon: '' }
      },
      {
        path: 'addReturnAd/:editId?',
        name: 'shopAddReturnAd',
        hidden: true,
        props: true,
        component: () => import('@/views/shop/addReturnAd'),
        meta: {
          title: '添加归还地址',
          icon: '',
          activeMenu: '/shop/returnAd',
          parentTitle: '归还地址设置',
          parentRouteName: 'shopReturnAd'
        }
      },
      {
        path: 'rentAd',
        name: 'shopRentAd',
        component: () => import('@/views/shop/rentAd'),
        meta: { title: '租赁地址', icon: '' }
      },
      {
        path: 'info',
        name: 'shopInfo',
        component: () => import('@/views/shop/info'),
        meta: { title: '店铺信息', icon: '' }
      },
      {
        path: 'shop',
        name: 'shopShop',
        component: () => import('@/views/shop/shop'),
        meta: { title: '旺铺管理', icon: '' }
      },
      {
        path: 'manage/:editId/:editName',
        name: 'shopManage',
        hidden: true,
        props: true,
        component: () => import('@/views/shop/manage'),
        meta: {
          title: '关联商品',
          icon: '',
          activeMenu: '/shop/shop',
          parentTitle: '旺铺管理',
          parentRouteName: 'shopShop'
        }
      }
    ]
  },
  {
    path: '/bill',
    name: 'bill',
    component: Layout,
    redirect: '/bill/list',
    meta: { title: '账单管理', icon: 'bill' },
    children: [
      {
        path: 'list',
        name: 'billList',
        component: () => import('@/views/bill/index'),
        meta: { title: '订单财务列表', icon: '' }
      },
      {
        path: 'bill',
        name: 'billBill',
        component: () => import('@/views/bill/bill'),
        meta: { title: '账单财务列表', icon: '' }
      },
      {
        path: 'hb',
        name: 'billHb',
        component: () => import('@/views/bill/hb'),
        meta: { title: '花呗分期账单', icon: '' }
      },
      {
        path: 'device',
        name: 'billDevice',
        component: () => import('@/views/bill/device'),
        meta: { title: '设备分期导出', icon: '' }
      },
      {
        path: 'payment',
        name: 'billPayment',
        component: () => import('@/views/bill/payment'),
        meta: { title: '回款导出', icon: '' }
      }
    ]
  },
  {
    path: '/overdue',
    name: 'overdue',
    component: Layout,
    redirect: '/overdue/list',
    meta: { title: '逾期管理', icon: 'overdue' },
    children: [
      {
        path: 'list',
        name: 'overdueList',
        component: () => import('@/views/overdue/index'),
        meta: { title: '逾期列表', icon: '' }
      }
    ]
  },
  {
    path: '/my',
    name: 'my',
    component: Layout,
    redirect: '/my/index',
    meta: { title: '账户管理', icon: 'user' },
    children: [
      {
        path: 'index',
        name: 'myIndex',
        component: () => import('@/views/account/index'),
        meta: { title: '我的账户', icon: '' }
      },
      {
        path: 'withdrawal',
        name: 'myWithdrawal',
        component: () => import('@/views/account/withdrawal'),
        meta: { title: '提现记录', icon: '' }
      },
      {
        path: 'flow',
        name: 'myFlow',
        component: () => import('@/views/account/flow'),
        meta: { title: '对账流水', icon: '' }
      },
      {
        path: 'withdraw/:withdrawId?',
        name: 'myWithdraw',
        hidden: true,
        props: true,
        component: () => import('@/views/account/withdraw'),
        meta: {
          title: '详情',
          icon: '',
          activeMenu: '/my/withdrawal',
          parentTitle: '提现记录',
          parentRouteName: 'myWithdrawal'
        }
      },
      {
        path: 'orderInfo',
        name: 'myOrderInfo',
        component: () => import('@/views/account/orderInfo'),
        meta: { title: '账户订单统计', icon: '' }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () =>
  new Router({
    mode: 'history', // require service support
    scrollBehavior(to, from, savedPosition) {
      if (savedPosition) {
        return savedPosition
      } else {
        return { x: 0, y: 0 }
      }
    },
    routes: constantRoutes
  })

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
